package fr.cephb.berkeleydb.dom;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

/**
 * An XML attribute
 * a pair of QName/value
 */
public class Attr
	{
	private QName qName;
	private String value;
	public Attr(QName qName,String value)
		{
		this.qName=qName;
		this.value=value;
		if(this.qName==null) throw new NullPointerException("qName");
		if(this.value==null) throw new NullPointerException("value");
		}
	
	public QName getQName()
		{
		return qName;
		}
	
	public String getValue()
		{
		return this.value;
		}
	public String getNamespaceURI()
		{
		return getQName().getNamespaceURI();
		}
	public String getLocalName()
		{
		return getQName().getLocalPart();
		}
	public String getNodeName()
		{
		return getQName().getNodeName();
		}
	public String getPrefix()
		{
		return getQName().getPrefix();
		}

	@Override
	public int hashCode()
		{
		final int prime = 31;
		int result = 1;
		result = prime * result + qName.hashCode();
		result = prime * result +value.hashCode();
		return result;
		}

	@Override
	public boolean equals(Object obj)
		{
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		Attr other = (Attr) obj;
		if (!qName.equals(other.qName)) return false;
		if (!value.equals(other.value)) return false;
		return true;
		}
	
	@Override
	public String toString()
		{
		return getNodeName()+"="+getValue();
		}
	
	public void write(XMLStreamWriter writer)
 	throws XMLStreamException
		{
		writer.writeAttribute(
				this.getPrefix(),
				this.getNamespaceURI(),
				this.getLocalName(),
				this.getValue()
				);
		}
	}
